package com.dingwei.lesyndrome.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dingwei.lesyndrome.R;
import com.dingwei.lesyndrome.adapter.PeripheralCollectAdapter;
import com.dingwei.lesyndrome.bean.CollectBean;
import com.dingwei.lesyndrome.mvp.presenter.PeripCollectPresenter;
import com.dingwei.lesyndrome.mvp.view.Contract;
import com.dingwei.lesyndrome.ui.activity.PeripheralDetailActivity;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.base.LazyLoadFragment;
import com.nikhilpanju.recyclerviewenhanced.RecyclerTouchListener;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created time 2018/3/7.
 * Creator 南柯一梦丶 on
 * Personal signature 余生还长 何必慌张
 * Page description :
 */

public class PeripheralCollectFragment extends LazyLoadFragment implements Contract.PeripheralCollectView {

    PeripCollectPresenter peripCollectPresenter = new PeripCollectPresenter(this);
    @Bind(R.id.m_recyclerView)
    RecyclerView mRecyclerView;
    @Bind(R.id.img_no_data)
    ImageView imgNoData;
    @Bind(R.id.tv_no_data)
    TextView tvNoData;
    @Bind(R.id.no_data_layout)
    RelativeLayout noDataLayout;
    @Bind(R.id.refresh_layout)
    SmartRefreshLayout refreshLayout;
    @Bind(R.id.no_net_btn)
    TextView noNetBtn;
    @Bind(R.id.no_net_layout)
    RelativeLayout noNetLayout;

    PeripheralCollectAdapter adapter;
    private RecyclerTouchListener onTouchListener;
    @Override
    protected int setContentView() {
        return R.layout.fragment_list;
    }

    @Override
    protected void setBindView() {
        ButterKnife.bind(this, view);
        initView();

    }

    private void initView() {
        adapter = new PeripheralCollectAdapter(BaseActivity.context);
        LinearLayoutManager layoutManager = new LinearLayoutManager(BaseActivity.context);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setNestedScrollingEnabled(false);
        mRecyclerView.setAdapter(adapter);
        onTouchListener = new RecyclerTouchListener(BaseActivity.context, mRecyclerView);
        onTouchListener.setClickable(new RecyclerTouchListener.OnRowClickListener() {
            @Override
            public void onRowClicked(int position) {
//                        showToast("Row " + (position + 1) + " clicked!");
                CollectBean.DataBean bean = adapter.getItem(position);
                intent  = new Intent(BaseActivity.context, PeripheralDetailActivity.class);
                intent.putExtra("id",bean.id);
                startActivity(intent);
            }

            @Override
            public void onIndependentViewClicked(int independentViewID, int position) {
//                        showToast("Button in row " + (position + 1) + " clicked!");
            }
        })
                .setSwipeOptionViews(R.id.tv_delete)
                .setSwipeable(R.id.rowFG, R.id.rowBG, new RecyclerTouchListener.OnSwipeOptionsClickListener() {
                    @Override
                    public void onSwipeOptionClicked(int viewID, int position) {
                        if (viewID == R.id.tv_delete) {
//                            CollectBean.DataBean bean = adapter.getItem(position);
//                            myCollectionPresenter.delete(bean.id,position);
                        }
                    }
                });

        refreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                page = page+1;
                peripCollectPresenter.initData(page,false,false);
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                peripCollectPresenter.initData(page,true,false);
            }
        });
    }

    @Override
    protected void lazyLoad() {
        refreshLayout.autoRefresh();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }


    public void showData(String state) {
        if (state.equals("success")) {
            noNetLayout.setVisibility(View.GONE);
            refreshLayout.setVisibility(View.VISIBLE);
            if (adapter.getAllData() != null && adapter.getAllData().size() > 0) {
                mRecyclerView.setVisibility(View.VISIBLE);
                noDataLayout.setVisibility(View.GONE);
            } else {
                mRecyclerView.setVisibility(View.GONE);
                noDataLayout.setVisibility(View.VISIBLE);
            }
        } else {
            refreshLayout.setVisibility(View.GONE);
            noNetLayout.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onFail() {
        showData("fail");
    }

    @Override
    public void onRefresh(CollectBean bean) {
        adapter.clear();
        adapter.addAll(bean.data);
        refreshLayout.finishRefresh();
        showData("success");
    }

    @Override
    public void onLoadMore(CollectBean bean) {
        adapter.addAll(bean.data);
        refreshLayout.finishLoadmore();
        showData("success");
    }
}
